<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 14.最长公共前缀
        编写一个函数来查找字符串数组中的最长公共前缀。

        如果不存在公共前缀，返回空字符串 ""。

        示例 1：

        输入：strs = ["flower","flow","flight"]
        输出："fl"
        示例 2：

        输入：strs = ["dog","racecar","car"]
        输出：""
        解释：输入不存在公共前缀。
     -->
    <script>
        var longestCommonPrefix = function (strs) {
            let strsStart = strs[0]
            for (i = 1; i < strs.length; i++) {
                for (j = strsStart.length - 1; j >= 0; j--) {
                    if (!strs[i].startsWith(strsStart)) {
                        strsStart = strsStart.slice(0, strsStart.length - 1)
                    } else {
                        j = -1
                    }
                }
            }
            console.log(strsStart || '')
        };
        longestCommonPrefix(["flower", "flow", "flight"])
        longestCommonPrefix(["dog", "racecar", "car"])
    </script>
</body>

</html>